<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\space\logic;

use plugin\saiadmin\basic\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\space\model\space;
use plugin\saiadmin\service\OpenSpoutWriter;

/**
 * 空间列表逻辑层
 */
class spaceLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new space();
    }
      public function getSpace($userId, $limit)
    {
        return $this->model
            ->rightJoin('eb_collection', 'eb_space.space_id = eb_collection.collection_id')
            ->field('eb_space.space_id,eb_space.space_title,eb_space.space_image,eb_space.rent_nature,eb_space.rent_price,eb_space.building_area,eb_space.address,eb_space.create_time')
            ->where('eb_collection.user_id', $userId)
            ->where('eb_collection.type', 4)
            // 修正拼写错误，将 ceeate_time 改为 create_time
            ->order('eb_collection.create_time desc')
            ->paginate($limit);
    }
    public function export($where = [])
    {
        $query = $this->search($where);
        // ->field('id,name,enterprise_id,sort,status,create_time');
        $data = $this->getAll($query);
        // var_dump($data);exit();

       
        $file_name = '空间数据.xlsx';
        $header = [ '项目信息', '空间分类','空间标题','空间地址','空间图片','空间多图','vr图片','VR链接', '租金（元/㎡/月）',

       '空间详情','创建时间'];
       
        $filter = [
            // 'activity_status' => [
            //     ['value' => 0, 'label' => '进行中'],
            //     ['value' => 1, 'label' => '已结束'],
            //     ['value' => 2, 'label' => '未开始']
            // ]
        ];
        $data_attr=[];
        foreach($data as $value){
           $data_attr[] = [
            'project_id_text'=>$value['project_id_text'],
            'space_title'=>$value['space_title'],
            'address'=>$value['address'],

            'space_class_text'=>$value['space_class_text'],
            'space_image'=>$value['space_image'],
            'space_images'=>empty($value['space_images'])?'':implode("\n",$value['space_images']),
            'vr_image'=>$value['vr_image'],

            'vr_link'=>$value['vr_link'],
            'rent_price'=>$value['rent_price'],
            'space_detail'=>$value['space_detail'],
            'create_time'=>$value['create_time']
           
           ];
        }

        $writer = new OpenSpoutWriter($file_name);
        $writer->setWidth([20, 20, 20, 20, 20, 50,20,20,20,50,20]);
        $writer->setHeader($header);
        $writer->setData($data_attr, null, $filter);
        $file_path = $writer->returnFile();
        return response()->download($file_path, urlencode($file_name));
    }

}
